package com.itheima.dubbo.mongodb.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.model.mongo.RecommendUser;
import com.itheima.commons.model.vo.PageResult;
import com.itheima.dubbo.interfaces.RecommendUserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询今日佳人
     *
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        RecommendUser recommendUser = mongoTemplate.findOne(Query.query(Criteria.where("toUserId").is(toUserId))
                        .with(Sort.by(Sort.Order.desc("score")))
                        .with(PageRequest.of(0, 1))
                , RecommendUser.class);
        return recommendUser;
    }

    /**
     * 首页推荐列表
     *
     * @param page
     * @param pagesize
     * @param toUserId
     * @return
     */
    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        // 在实际开发中如果有些需求 是模棱两可的  怎么做都行
        // 我们要去咨询产品 确定一个方案
        List<RecommendUser> recommendUsers = mongoTemplate.find(Query.query(Criteria.where("toUserId").is(toUserId))
                        .with(Sort.by(Sort.Order.desc("score")))
                        .with(PageRequest.of(page - 1, pagesize))
                , RecommendUser.class);
        // 凡是涉及到分页的查询
        // 我们一般都要把分页相关参数都返回
        // 当前页码  每页显示的条数 每页的数据 总的页码 总的条数
        Long counts = mongoTemplate.count(new Query(), RecommendUser.class);
        return new PageResult(page, pagesize, counts, recommendUsers);
    }

    @Override
    public RecommendUser queryScoreByUserId(Long toUserId, Long userId) {
        return mongoTemplate.findOne(Query.query(
                Criteria.where("toUserId").is(toUserId)
                        .and("userId").is(userId)), RecommendUser.class);
    }

    @Override
    public Long queryMatchUserCount(Long userId) {
        return mongoTemplate.count(Query.query(Criteria.where("toUserId").is(userId)), RecommendUser.class);
    }

    // 卡片数据查询
    @Override
    public List<RecommendUser> queryCardsList(Long toUserId, Integer counts, List<Long> exUserIds) {
        // 从MongoDB中随机抽样数据
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(Aggregation.newAggregation(RecommendUser.class,
                Aggregation.match(Criteria.where("toUserId").is(toUserId)),
                Aggregation.match(Criteria.where("userId").nin(exUserIds))
                , Aggregation.sample(counts)), RecommendUser.class);
        List<RecommendUser> mappedResults = aggregate.getMappedResults();
        return mappedResults;
    }
}
